package bizapi.sinosoft.com.provider.stock;

import bizapi.sinosoft.com.provider.CommonResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weibo.api.motan.config.springsupport.annotation.MotanReferer;
import jd.api.request.product.StockForOrderEntity;
import jd.api.service.product.JdProductApi;
import jd.api.vo.product.StockNewResultVo;
import jd.api.vo.product.StockVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import utils.Lang;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/2/9.
 * 批量获取库存
 */
@RestController
@RequestMapping("api/stock")
public class ProductStockCheckController {
    @MotanReferer
    JdProductApi jdProductApi;
    @RequestMapping(value = "getNewStockById",method = RequestMethod.POST)
    public CommonResponse<List<StockNewResultVo>> getNewStockById(String skuNums, String area) {
        CommonResponse<List<StockNewResultVo>> commonResponse = new CommonResponse();
        try {
            JSONArray jSONArray = JSONArray.parseArray(skuNums);
            StockVO stockVO = new StockVO();
            StockForOrderEntity stockForOrderEntity = new StockForOrderEntity();
            List<StockVO> skuNumsList = new ArrayList<>();
            for (int i=0;i<jSONArray.size();i++){
                JSONObject jsonObject = jSONArray.getJSONObject(i);
                Long skuId = jsonObject.getLong("skuId");
                Integer num = jsonObject.getInteger("num");
                stockVO.setSkuId(skuId);
                stockVO.setNum(num);
                skuNumsList.add(stockVO);
            }
            stockForOrderEntity.setArea(area);
            stockForOrderEntity.setSkuNums(skuNumsList);
            List<StockNewResultVo> stockNewResultVos = jdProductApi.getStockForOrder(stockForOrderEntity);
            commonResponse.setResult(stockNewResultVos);
            if(Lang.isEmpty(stockNewResultVos)){
                commonResponse.setSuccess(false);
            }
            else{
            commonResponse.setResult(stockNewResultVos);
            commonResponse.setSuccess(true);
            }
            return commonResponse;
        } catch (Exception e) {
            commonResponse.setResultMessage(e.getMessage());
            commonResponse.setSuccess(false);
            return commonResponse;
        }
    }
}
